<?php
$this->load->library('My_Controller');
class Tasks extends My_Controller {

	function __construct(){
        parent::__construct();
        $this->load->model('task');
        
    }
    
	function index(){
		$this->load->view('tasks_list');
	}
	
	function upsert_task(){
		$id						= $this->input->post('id');
		$subject 				= $this->input->post('subject');
		$description 			= $this->input->post('description');
		$priority 				= $this->input->post('priority');
		$status 				= $this->input->post('status');
		$estimated_hours 		= $this->input->post('estimated_hours');
		$worked_hours 			= $this->input->post('worked_hours');
		$percentaje_completed 	= $this->input->post('percentaje_completed');
		
		
		if($id != NULL && $id != ''){
			$this->task->update_task($id, $subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
		}
		else{
			$insertId = $this->task->create_task($subject, $description, $priority, $status, $estimated_hours, $worked_hours, $percentaje_completed);
		}
		
		redirect('/index.php/tasks', 'refresh');
	}
	
	function create() {
		$data['general'] = array('priority_options' => array('1' => '1', '2' => '2', '3' => '3'),
										 'status_options' 	=> array('0' => '0', '1' => '1', '2' => '2'),
										 'completed_options'=> array('0' => '0', '1' => '1', '2' => '2')
										);
										
		$this->load->view('task_upsert', $data);
	}
	
	function delete($id) {
		$this->task->delete_task($id);
		redirect('/index.php/tasks', 'refresh');
	}
	
	function update($id = NULL) {
		$data['general'] = array('priority_options' => array('1' => '1', '2' => '2', '3' => '3'),
										 'status_options' 	=> array('0' => '0', '1' => '1', '2' => '2'),
										 'completed_options'=> array('0' => '0', '1' => '1', '2' => '2')
										);
		if($id != NULL && $id != ''){
			$task = $this->task->get_task($id);
			$data['task'] = $task[0];
		}
		
		$this->load->view('task_upsert', $data);
	}
	
	function get(){
		$id = $_POST['id'];
		$result = NULL;
		
		if($id != NULL && $id != ''){
			$task = $this->task->get_task($id);
			$result = $task[0];
		}
		echo json_encode($result);
	}
	
	function view_task($id) {
		
	}
	
	function get_tasks() {
		$page 			= $this->input->post('page');
		$limit 			= $this->input->post('rows');
		$order_by_field = $this->input->post('sidx');
		$order_by_type 	= $this->input->post('sord');
		
		if(!$order_by_field) $sidx =1;
		
		$count = $this->task->get_tasks_count();

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		
		$responce->page 	= $page;
		$responce->total 	= $total_pages;
		$responce->records 	= $count;
		
		$tasks = $this->task->get_tasks($order_by_field, $order_by_type, $start , $limit); 
		
		for($i=0; $i< sizeof($tasks); $i++) {
			$responce->rows[$i]['id'] = $tasks[$i]['id'];
			$responce->rows[$i]['cell'] = array($tasks[$i]['id'], 
												$tasks[$i]['subject'], 
												$tasks[$i]['priority'],
												$tasks[$i]['creation_date'],
												$tasks[$i]['status'],
												$tasks[$i]['estimated_hours'], 
												$tasks[$i]['worked_hours'], 
												$tasks[$i]['percentaje_completed'], 'action');
		}
     	
		echo json_encode($responce);
	}
}
?>